//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SysOrg;
import com.ruoyi.system.domain.vo.TreeSelect;
import com.ruoyi.system.mapper.SysOrgMapper;
import com.ruoyi.system.service.ISysOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysOrgServiceImpl implements ISysOrgService {
    @Autowired
    private SysOrgMapper sysOrgMapper;
    @Autowired
    private TokenService tokenService;

    //public SysOrgServiceImpl() {
    //}

    public SysOrg selectSysOrgByOrgNo(String orgNo) {
        return this.sysOrgMapper.selectSysOrgByOrgNo(orgNo);
    }

    public List<SysOrg> selectSysOrgList(SysOrg sysOrg) {
        if (StringUtils.isBlank(sysOrg.getOrgNo())) {
            sysOrg.setOrgNo(this.tokenService.getLoginUser().getSysUser().getOrgNo());
        }

        List<SysOrg> sysOrgs = this.sysOrgMapper.selectSysOrgList(sysOrg);
        return sysOrgs;
    }

    public SysOrg selectOrgNoByUserId(Long userId) {
        SysOrg sysOrgs = this.sysOrgMapper.selectOrgNoByUserId(userId);
        return sysOrgs;
    }

    public List<TreeSelect> buildOrgTreeSelect(List<SysOrg> orgs) {
        List<SysOrg> orgTrees = this.buildOrgTree(orgs);
        return (List)orgTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    public List<SysOrg> buildOrgTree(List<SysOrg> orgs) {
        List<SysOrg> returnList = new ArrayList();
        List<String> tempList = new ArrayList();
        Iterator var4 = orgs.iterator();

        SysOrg org;
        while(var4.hasNext()) {
            org = (SysOrg)var4.next();
            tempList.add(org.getOrgNo());
        }

        var4 = orgs.iterator();

        while(var4.hasNext()) {
            org = (SysOrg)var4.next();
            if (!tempList.contains(org.getpOrgNo())) {
                this.recursionFn(orgs, org);
                ((List)returnList).add(org);
            }
        }

        if (((List)returnList).isEmpty()) {
            returnList = orgs;
        }

        return (List)returnList;
    }

    private void recursionFn(List<SysOrg> list, SysOrg o) {
        List<SysOrg> childList = this.getChildList(list, o);
        o.setChildren(childList);
        Iterator var4 = childList.iterator();

        while(var4.hasNext()) {
            SysOrg oChild = (SysOrg)var4.next();
            if (this.hasChild(list, oChild)) {
                this.recursionFn(list, oChild);
            }
        }

    }

    private List<SysOrg> getChildList(List<SysOrg> list, SysOrg o) {
        List<SysOrg> olist = new ArrayList();
        Iterator<SysOrg> io = list.iterator();

        while(io.hasNext()) {
            SysOrg n = (SysOrg)io.next();
            if (StringUtils.isNotNull(n.getpOrgNo()) && n.getpOrgNo().equals(o.getOrgNo())) {
                olist.add(n);
            }
        }

        return olist;
    }

    private boolean hasChild(List<SysOrg> list, SysOrg o) {
        return this.getChildList(list, o).size() > 0;
    }

    public int insertSysOrg(SysOrg sysOrg) {
        String orgNo = sysOrg.getOrgNo();
        String pOrgNo = sysOrg.getpOrgNo();
        StringBuilder orgTreeStr = new StringBuilder();
        orgTreeStr.append(pOrgNo).append(",").append(orgNo);
        if (!orgNo.startsWith("65")) {
            throw new ServiceException("编码格式有误，请确定编码格式");
        } else {
            String a = "111";

            while(true) {
                pOrgNo = this.sysOrgMapper.selectPOrgNo(pOrgNo);
                if ("65101".equals(pOrgNo)) {
                    orgTreeStr.insert(0, "65101,");
                    sysOrg.setOrgNo(orgNo);
                    sysOrg.setOrgTree(orgTreeStr.toString());
                    return this.sysOrgMapper.insertSysOrg(sysOrg);
                }

                orgTreeStr.insert(0, ",").insert(0, pOrgNo);
            }
        }
    }

    public int updateSysOrg(SysOrg sysOrg) {
        return this.sysOrgMapper.updateSysOrg(sysOrg);
    }

    public int deleteSysOrgByOrgNos(String[] orgNos) {
        return this.sysOrgMapper.deleteSysOrgByOrgNos(orgNos);
    }

    public int deleteSysOrgByOrgNo(String orgNo) {
        return this.sysOrgMapper.deleteSysOrgByOrgNo(orgNo);
    }

    public String checkOrgNameUnique(SysOrg org) {
        String orgNo = StringUtils.isNull(org.getOrgNo()) ? "-1" : org.getOrgNo();
        SysOrg info = this.sysOrgMapper.checkOrgNameUnique(org.getOrgName(), org.getpOrgNo());
        return StringUtils.isNotNull(info) && !info.getOrgNo().equals(orgNo) ? "1" : "0";
    }

    public String checkOrgNoUnique(SysOrg org) {
        String orgNo = StringUtils.isNull(org.getOrgNo()) ? "-1" : org.getOrgNo();
        String info = this.sysOrgMapper.checkOrgNoUnique(orgNo);
        return StringUtils.isNotNull(info) && info.equals(orgNo) ? "1" : "0";
    }

    public String checkOrgType(String orgNo) {
        String orgType = this.sysOrgMapper.checkOrgType(orgNo);
        return orgType;
    }

    @Async
    public void checkOrgDataScope(String orgNo) {
        if (!SysUser.isAdmin(SecurityUtils.getUserId())) {
            SysOrg org = new SysOrg();
            org.setOrgNo(orgNo);
            List<SysOrg> orgs = ((SysOrgServiceImpl)SpringUtils.getAopProxy(this)).selectSysOrgList(org);
            if (StringUtils.isEmpty(orgs)) {
                throw new ServiceException("没有权限访问供电部门数据！");
            }
        }

    }

    public boolean hasChildByOrgNo(String orgNo) {
        int result = this.sysOrgMapper.hasChildByOrgNo(orgNo);
        return result > 0;
    }

    public String selectPOrgNo(String orgNo) {
        return this.sysOrgMapper.selectPOrgNo(orgNo);
    }
}
